
<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="卷宗号:" prop="caseNumber">
          <el-input v-model="formData.caseNumber" :clearable="false"  placeholder="请输入卷宗号" />
       </el-form-item>
        <el-form-item label="年份:" prop="year">
          <el-input v-model="formData.year" :clearable="false"  placeholder="请输入年份" />
       </el-form-item>
        <el-form-item label="案由:" prop="cause">
          <el-input v-model="formData.cause" :clearable="false"  placeholder="请输入案由" />
       </el-form-item>
        <el-form-item label="基本事实:" prop="basicFacts">
          <RichEdit v-model="formData.basicFacts"/>
       </el-form-item>
        <el-form-item label="查明事实:" prop="judicialFacts">
          <RichEdit v-model="formData.judicialFacts"/>
       </el-form-item>
        <el-form-item label="裁判理由:" prop="judgmentReason">
          <RichEdit v-model="formData.judgmentReason"/>
       </el-form-item>
        <el-form-item label="法律依据:" prop="legalBasis">
          <el-input v-model="formData.legalBasis" :clearable="false"  placeholder="请输入法律依据" />
       </el-form-item>
        <el-form-item label="涉案金额:" prop="amountInvolved">
          <el-input-number v-model="formData.amountInvolved" :precision="2" :clearable="false"></el-input-number>
       </el-form-item>
        <el-form-item label="证据类型:" prop="evidenceType">
          <el-input v-model="formData.evidenceType" :clearable="false"  placeholder="请输入证据类型" />
       </el-form-item>
        <el-form-item label="裁判结果:" prop="judgmentResult">
          <RichEdit v-model="formData.judgmentResult"/>
       </el-form-item>
        <el-form-item label="行为人特质:" prop="perpetratorTrait">
          <el-input v-model="formData.perpetratorTrait" :clearable="false"  placeholder="请输入行为人特质" />
       </el-form-item>
        <el-form-item label="罪量要素:" prop="crimeElements">
          <el-input v-model="formData.crimeElements" :clearable="false"  placeholder="请输入罪量要素" />
       </el-form-item>
        <el-form-item label="减轻情节:" prop="mitigatingCircumstances">
          <el-input v-model="formData.mitigatingCircumstances" :clearable="false"  placeholder="请输入减轻情节" />
       </el-form-item>
        <el-form-item label="因变量:" prop="dependentVariables">
          <el-input v-model="formData.dependentVariables" :clearable="false"  placeholder="请输入因变量" />
       </el-form-item>
        <el-form-item label="入罪后量刑:" prop="sentenceAfterConviction">
          <el-input v-model="formData.sentenceAfterConviction" :clearable="false"  placeholder="请输入入罪后量刑" />
       </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createCaseManagement,
  updateCaseManagement,
  findCaseManagement
} from '@/api/flaud/caseManagement'

defineOptions({
    name: 'CaseManagementForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'
// 富文本组件
import RichEdit from '@/components/richtext/rich-edit.vue'


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const formData = ref({
            caseNumber: '',
            year: '',
            cause: '',
            basicFacts: '',
            judicialFacts: '',
            judgmentReason: '',
            legalBasis: '',
            amountInvolved: 0,
            evidenceType: '',
            judgmentResult: '',
            perpetratorTrait: '',
            crimeElements: '',
            mitigatingCircumstances: '',
            dependentVariables: '',
            sentenceAfterConviction: '',
        })
// 验证规则
const rule = reactive({
               caseNumber : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               year : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               cause : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               basicFacts : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               judicialFacts : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               judgmentReason : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               legalBasis : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               amountInvolved : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               evidenceType : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               judgmentResult : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               perpetratorTrait : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               crimeElements : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               mitigatingCircumstances : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               dependentVariables : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
               sentenceAfterConviction : [{
                   required: true,
                   message: '',
                   trigger: ['input','blur'],
               }],
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findCaseManagement({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
}

init()
// 保存按钮
const save = async() => {
      btnLoading.value = true
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return btnLoading.value = false
            let res
           switch (type.value) {
             case 'create':
               res = await createCaseManagement(formData.value)
               break
             case 'update':
               res = await updateCaseManagement(formData.value)
               break
             default:
               res = await createCaseManagement(formData.value)
               break
           }
           btnLoading.value = false
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
